/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.FaceDraft;
import com.yami.shop.bean.enums.DraftStatusEnum;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.FaceDraftService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;


/**
 * 草稿单管理控制器
 * @author
 */
@RestController
@RequestMapping("/admin/faceDraft")
public class FaceDraftController {

    @Autowired
    private FaceDraftService faceDraftService;

    /**
     * 分页获取草稿单列表
     */
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('admin:faceDraft:page')")
    public ServerResponseEntity<IPage<FaceDraft>> page(FaceDraft faceDraft, PageParam<FaceDraft> page) {
        IPage<FaceDraft> faceDraftPage = faceDraftService.page(page, new LambdaQueryWrapper<FaceDraft>()
                .like(StrUtil.isNotBlank(faceDraft.getDraftName()), FaceDraft::getDraftName, faceDraft.getDraftName())
                .eq(faceDraft.getUserId() != null, FaceDraft::getUserId, faceDraft.getUserId())
                .eq(faceDraft.getDraftStatus() != null, FaceDraft::getDraftStatus, faceDraft.getDraftStatus()));
        return ServerResponseEntity.success(faceDraftPage);
    }

    /**
     * 获取草稿单详情
     */
    @GetMapping("/info/{draftId}")
//    @PreAuthorize("@pms.hasPermission('admin:faceDraft:info')")
    public ServerResponseEntity<FaceDraft> info(@PathVariable("draftId") String draftId) {
        FaceDraft faceDraft = faceDraftService.getById(draftId);
        return ServerResponseEntity.success(faceDraft);
    }

    /**
     * 新增草稿单
     */
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceDraft:save')")
    public ServerResponseEntity<Void> save(@RequestBody FaceDraft faceDraft) {
        faceDraft.setCreateTime(new Date());
        faceDraft.setUpdateTime(new Date());
        faceDraftService.save(faceDraft);
        return ServerResponseEntity.success();
    }

    /**
     * 修改草稿单
     */
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceDraft:update')")
    public ServerResponseEntity<Void> update(@RequestBody FaceDraft faceDraft) {
        faceDraft.setUpdateTime(new Date());
        faceDraftService.updateById(faceDraft);
        return ServerResponseEntity.success();
    }

    /**
     * 删除草稿单
     */
    @DeleteMapping
//    @PreAuthorize("@pms.hasPermission('admin:faceDraft:delete')")
    public ServerResponseEntity<Void> delete(@RequestBody String[] draftIds) {
        faceDraftService.removeByIds(Arrays.asList(draftIds));
        return ServerResponseEntity.success();
    }

    /**
     * 更新草稿单状态
     */
    @PutMapping("/status")
//    @PreAuthorize("@pms.hasPermission('admin:faceDraft:update')")
    public ServerResponseEntity<Void> updateStatus(@RequestBody FaceDraft faceDraft) {
        FaceDraft updateFaceDraft = new FaceDraft();
        updateFaceDraft.setDraftId(faceDraft.getDraftId());
        updateFaceDraft.setDraftStatus(faceDraft.getDraftStatus());
        updateFaceDraft.setUpdateTime(new Date());
        faceDraftService.updateById(updateFaceDraft);
        return ServerResponseEntity.success();
    }

    /**
     * 确认排单
     */
    @PostMapping("/confirmSchedule")
//    @PreAuthorize("@pms.hasPermission('admin:faceDraft:confirmSchedule')")
    public ServerResponseEntity<Long> confirmSchedule(
            @RequestParam("draftId") Long draftId,
            @RequestParam("orderAmount") java.math.BigDecimal orderAmount) {
        Long orderId = faceDraftService.confirmSchedule(draftId, orderAmount);
        return ServerResponseEntity.success(orderId);
    }

    /**
     * 分页查询草稿单（带排单状态）
     */
    @GetMapping("/pageByScheduleStatus")
//    @PreAuthorize("@pms.hasPermission('admin:faceDraft:page')")
    public ServerResponseEntity<IPage<FaceDraft>> pageByScheduleStatus(
            @RequestParam(value = "scheduleStatus", required = false) Integer scheduleStatus,
            @RequestParam(value = "status", required = false) Integer status,
            PageParam<FaceDraft> page) {

        IPage<FaceDraft> faceDraftPage = faceDraftService.page(page, new LambdaQueryWrapper<FaceDraft>()
                .eq(scheduleStatus != null, FaceDraft::getScheduleStatus, scheduleStatus)
                .eq(status != null, FaceDraft::getDraftStatus, status));
        return ServerResponseEntity.success(faceDraftPage);
    }
}